package dashboard.controllers;

import java.util.Arrays;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;

import com.bj58.argo.ActionResult;
import com.bj58.argo.Model;
import com.bj58.argo.annotations.GET;
import com.bj58.argo.annotations.POST;
import com.bj58.argo.annotations.Path;
import com.bj58.argo.controller.AbstractController;

import dashboard.cache.ServiceBeanFactory;
import dashboard.constant.EnumCate;
import dashboard.constant.IncomeCateConst;
import dashboard.constant.IncomeCityConst;
import dashboard.constant.IncomeNetflowCityConst;
import dashboard.dbentity.IncomeCity;
import dashboard.service.AbstractBaseService;
import dashboard.utils.MyServletRequestUtils;

@Path("/income")
public class IncomeController extends AbstractController {
	private AbstractBaseService<IncomeCity> imCityService = ServiceBeanFactory.get(IncomeCity.class,null);
	
	/**
	 * 收入看板-城市
	 * @return
	 * @author zhaoxiang 2015-04-30
	 */
	@Path("/citycate")
	@GET
	@POST
	public ActionResult dashboard() {
		EnumCate cateEnum = null;
		try {
			HttpServletRequest request = beat().getRequest();
			//检查业务线权限
			String[] businessOpers = "total,zhaopin,fangchan,huangye,ershou,ershouche".split(",");
			
			String businessName = MyServletRequestUtils.getStringParameter(request, "businessName","total");
			
			if("total".equals(businessName)){
				businessName="zhaopin";
				cateEnum = EnumCate.valueOf(businessName);
			}else{
				cateEnum = EnumCate.valueOf(businessName);
			}
			
			if("total".equals(businessOpers[0])){
				businessOpers = Arrays.copyOfRange(businessOpers, 1, businessOpers.length);
			}
			//目前的最大日期
			String maxDate = imCityService.getMaxDate();
			Model model = beat().getModel();
			model.add("maxDate", maxDate);
			//业务线 中英文名
			model.add("businessName", businessName);
			model.add("businessName_CN", cateEnum.getDBName());
			model.add("business_CN_Map",EnumCate.business_CN_Map);
			//业务线权限
			model.add("businessList",businessOpers);
			model.add("zhixiaoCityList",IncomeCityConst.ZX_CityList);	//直销城市
			if(!StringUtils.equals(businessName, "ershouche")){
				model.add("cateList",IncomeCateConst.getCateListByBusiness(businessName));	//重点类别，下拉选择
				model.add("defautCate",IncomeCateConst.getCateListByBusiness(businessName).get(0));	//重点类别，下拉选择}
			}
			model.add("cityCmp_citys","北京,上海"); 	//默认对比城市
//			model.add("consume_city_indexs","xhzq,chongzhi"); //默认对比指标
		} catch (Exception e) {
			logger.error(null, e);
			return view("null");
		}
		return view("income/cityCate");
	}
	

	/**
	 * 收入看板-流量
	 * @return
	 * @author zhaoxiang 2015-04-30
	 */
	@Path("/netflow")
	@GET
	@POST
	public ActionResult netflow() {
		EnumCate cateEnum = null;
		try {
			HttpServletRequest request = beat().getRequest();
			String[] businessOpers = "total,zhaopin,fangchan,huangye,ershou,ershouche".split(",");
			
			String businessName = MyServletRequestUtils.getStringParameter(request, "businessName","total");
			
			if("total".equals(businessName)){
				businessName="zhaopin";
				cateEnum = EnumCate.valueOf(businessName);
			}else{
				cateEnum = EnumCate.valueOf(businessName);
			}
			
			if("total".equals(businessOpers[0])){
				businessOpers = Arrays.copyOfRange(businessOpers, 1, businessOpers.length);
			}
			//目前的最大日期
			Model model = beat().getModel();
//			String maxDate = incomeCityNetflow.getMaxDate();
//			model.add("maxDate", maxDate);
			//业务线 中英文名
			model.add("businessName", businessName);
			model.add("businessName_CN", cateEnum.getDBName());
			model.add("business_CN_Map",EnumCate.business_CN_Map);
			//业务线权限
			model.add("businessList",businessOpers);
			model.add("zhixiaoCityList",IncomeCityConst.ZX_CityList);	//直销城市
			if(!StringUtils.equals(businessName, "ershouche")){
				model.add("cateList",IncomeCateConst.getCateListByBusiness(businessName));	//重点类别，下拉选择
				model.add("defautCate",IncomeCateConst.getCateListByBusiness(businessName).get(0));	//重点类别，下拉选择}
			}
			model.add("cityCmp_citys","北京,上海"); 	//默认对比城市
			model.add("indexList_EN", IncomeNetflowCityConst.indexList_EN); 	//指标
			model.add("indexList_CN", IncomeNetflowCityConst.indexList_CN); 	//中文显示指标
//			model.add("consume_city_indexs","xhzq,chongzhi"); //默认对比指标
		} catch (Exception e) {
			logger.error(null, e);
			return view("null");
		}
		return view("income/netflowCityCate");
	}
	
	/**
	 * 收入看板-测试页
	 * @return
	 * @author zhaoxiang 2015-05-20
	 */
	@Path("/test")
	@POST
	@GET
	public ActionResult test() {
		EnumCate cateEnum = null;
		try {
			HttpServletRequest request = beat().getRequest();
			String[] businessOpers = "total,zhaopin,fangchan,huangye,ershou,ershouche".split(",");
			
			String businessName = MyServletRequestUtils.getStringParameter(request, "businessName","total");
			
			if("total".equals(businessName)){
				businessName="zhaopin";
				cateEnum = EnumCate.valueOf(businessName);
			}else{
				cateEnum = EnumCate.valueOf(businessName);
			}
			
			if("total".equals(businessOpers[0])){
				businessOpers = Arrays.copyOfRange(businessOpers, 1, businessOpers.length);
			}
			//目前的最大日期
			String maxDate = imCityService.getMaxDate();
			Model model = beat().getModel();
			model.add("maxDate", maxDate);
			//业务线 中英文名
			model.add("businessName", businessName);
			model.add("businessName_CN", cateEnum.getDBName());
			model.add("business_CN_Map",EnumCate.business_CN_Map);
			//业务线权限
			model.add("businessList",businessOpers);
			model.add("zhixiaoCityList",IncomeCityConst.ZX_CityList);	//直销城市
			if(!StringUtils.equals(businessName, "ershouche")){
				model.add("cateList",IncomeCateConst.getCateListByBusiness(businessName));	//重点类别，下拉选择
				model.add("defautCate",IncomeCateConst.getCateListByBusiness(businessName).get(0));	//重点类别，下拉选择}
			}
			model.add("cityCmp_citys","北京,上海"); 	//默认对比城市
			model.add("consume_city_indexs","xhzq,chongzhi"); //默认对比指标
		} catch (Exception e) {
			logger.error(null, e);
			return view("null");
		}		return view("income/income_test");
	}
	
	
	
}
